package java6681.lesson08;

import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.*;

import java.io.File;
import java.io.FilenameFilter;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;

/**
 * @author Junichiro
 * @description
 * @date 2021-04-25 11:58
 **/
public class FileFilter6681Controller {

    @FXML
    private TextField tfDir;

    @FXML
    private Label lblMsg;

    @FXML
    private ListView<String> lvFiles;

    @FXML
    private RadioButton rb1;

    @FXML
    private ToggleGroup tg;

    @FXML
    private RadioButton rb2;

    @FXML
    private RadioButton rb3;

    static ArrayList<String> array = new ArrayList<>();

    @FXML
    private void initialize() {
        tg.selectedToggleProperty().addListener(this::listShow);
    }

    private void listShow(ObservableValue<? extends Toggle> observableValue, Toggle toggle, Toggle toggle1) {
        ObservableList<String> items = lvFiles.getItems();
        RadioButton rb = (RadioButton) toggle1;
        switch (rb.getText()) {
            case "方法一：匿名内部类":
                inner6681(tfDir.getText());
                break;
            case "方法二：Lambda表达式":
                reference6681();
                break;
            case "方法三：方法引用":lambda6681();

                break;
        }

    }

    @FXML
    void listFile() {

    }

    //F:\JavaFX\Java6681\demo
    /*用匿名内部类实现文件过滤器接口FilenameFilter*/
    public void inner6681(String dir) {
        ObservableList<String> items = lvFiles.getItems();
        items.clear();
        File file = new File(dir);
        FilenameFilter filter1 = new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                return name.endsWith(".java");
            }
        };
        int count1 = 0;
        File[] files1 = file.listFiles(filter1);
        for (File file1 : files1) {
            count1++;
            array.add(String.valueOf(file1));
        }
        items.addAll("(1)后缀为java的文件：" + count1 + "\n");
        for (String s : array) {
            items.addAll(s);
        }

        FilenameFilter filter2 = new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                return new File(dir, name).length() / 1024 > 2;
            }
        };
        array.clear();
        int count2 = 0;
        File[] files2 = file.listFiles(filter2);
        for (File file1 : files2) {
            count2++;
            array.add(String.valueOf(file1));
        }
        items.addAll("(2)大于2K的文件：" + count2 + "\n");
        for (String s : array) {
            items.addAll(s);
        }

        LocalDateTime rq = LocalDateTime.of(2021, 4, 17, 0, 0);
        long milli = rq.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
        FilenameFilter filter3 = new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                return new File(dir, name).lastModified() > milli;
            }
        };
        array.clear();
        int count3 = 0;
        File[] files3 = file.listFiles(filter3);
        for (File file1 : files3) {
            count3++;
            array.add(String.valueOf(file1));
        }
        items.addAll("(3)3天前到现在修改过的文件：" + count3 + "\n");
        for (String s : array) {
            items.addAll(s);
        }
    }

    /*用Lambda表达式实现文件过滤器接口FilenameFilter*/
    private void lambda6681() {
        ObservableList<String> items = lvFiles.getItems();
        int list = list(tfDir.getText(), (dir, name) -> new File(dir, name).length() / 1024 > 2);
        int list1 = list(tfDir.getText(), (dir, name) -> name.endsWith(".java"));
        LocalDateTime rq = LocalDateTime.of(2021, 4, 17, 0, 0);
        long milli = rq.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
        int list2 = list(tfDir.getText(), (dir, name) -> new File(dir, name).lastModified() > milli);
        items.clear();

        items.addAll("(1)后缀为java的文件：" + list1);
        list(tfDir.getText(), (dir, name) -> name.endsWith(".java"));

        items.addAll("(2)大于2K的文件：" + list);
        list(tfDir.getText(), (dir, name) -> new File(dir, name).length() / 1024 > 2);

        items.addAll("(3)3天前到现在修改过的文件：" + list2);
        list(tfDir.getText(), (dir, name) -> new File(dir, name).lastModified() > milli);
    }

    /*用方法引用实现文件过滤器接口FilenameFilter*/
    private void reference6681() {
        ObservableList<String> items = lvFiles.getItems();
        int list1 = list(tfDir.getText(), this::test);
        int list2 = list(tfDir.getText(), this::test2);
        int list3 = list(tfDir.getText(), this::test3);
        items.clear();
        items.addAll("(1)后缀为java的文件：" + list1);
        list(tfDir.getText(), this::test);
        items.addAll("(2)大于2K的文件：" + list2);
        list(tfDir.getText(), this::test2);
        items.addAll("(3)3天前到现在修改过的文件：" + list3);
        list(tfDir.getText(), this::test3);
    }

    private boolean test(File file, String s) {
        return s.endsWith(".java");
    }

    private boolean test2(File file, String s) {
        return file.length() / 1024 > 2;
    }

    private boolean test3(File file, String s) {
        LocalDateTime rq = LocalDateTime.of(2021, 4, 22, 0, 0);
        long milli = rq.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
        return file.lastModified() > milli;
    }

    private int list(String dir, FilenameFilter filter) {
        ObservableList<String> items = lvFiles.getItems();
        int count = 0;
        File file = new File(dir);
        File[] files = file.listFiles(filter);

        for (File f : files) {
            count++;
            items.addAll(String.valueOf(f));
        }
        return count;
    }
}
